﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tunnel.DevicesControls
{
    public partial class Fan : DeviceBase
    {
        string deviceID;
        string shortName;
        
        string typeID;
        string devicePos;
        int picX;
        int picY;
        string stationCode;
        string groupID;
        string commType;
        string protocol;
        string port;
        int factoryID;
        int controllable;
        DateTime updateTime;
        string updateBy;
        string standardID;
        string tunnel;

        string typeName;
        string subSys;

        string statusID;
        DateTime rsTime;
        string statusDescription;

        string paramAddr;
        string paramValue;
        DateTime cmdTime;
        string runned;

        string zzid;
        string fzid;
        string tzid;

        private event EventHandler DeviceIdChanged;

        public string DeviceID
        {
            get
            {
                return deviceID;
            }

            set
            {
                deviceID = value;
                DeviceIdChanged(this, new EventArgs());

            }
        }

        public string ShortName
        {
            get
            {
                return shortName;
            }

            set
            {
                shortName = value;
            }
        }

        

        public string TypeID
        {
            get
            {
                return typeID;
            }

            set
            {
                typeID = value;
            }
        }

        public string DevicePos
        {
            get
            {
                return devicePos;
            }

            set
            {
                devicePos = value;
            }
        }

        public int PicX
        {
            get
            {
                return picX;
            }

            set
            {
                picX = value;
            }
        }

        public int PicY
        {
            get
            {
                return picY;
            }

            set
            {
                picY = value;
            }
        }

        public string StationCode
        {
            get
            {
                return stationCode;
            }

            set
            {
                stationCode = value;
            }
        }

        public string GroupID
        {
            get
            {
                return groupID;
            }

            set
            {
                groupID = value;
            }
        }

        public string CommType
        {
            get
            {
                return commType;
            }

            set
            {
                commType = value;
            }
        }

        public string Protocol
        {
            get
            {
                return protocol;
            }

            set
            {
                protocol = value;
            }
        }

        public string Port
        {
            get
            {
                return port;
            }

            set
            {
                port = value;
            }
        }

        public int FactoryID
        {
            get
            {
                return factoryID;
            }

            set
            {
                factoryID = value;
            }
        }

        public int Controllable
        {
            get
            {
                return controllable;
            }

            set
            {
                controllable = value;
            }
        }

        public DateTime UpdateTime
        {
            get
            {
                return updateTime;
            }

            set
            {
                updateTime = value;
            }
        }

        public string UpdateBy
        {
            get
            {
                return updateBy;
            }

            set
            {
                updateBy = value;
            }
        }

        public string StandardID
        {
            get
            {
                return standardID;
            }

            set
            {
                standardID = value;
            }
        }

        public string Tunnel
        {
            get
            {
                return tunnel;
            }

            set
            {
                tunnel = value;
            }
        }

        public string TypeName
        {
            get
            {
                return typeName;
            }

            set
            {
                typeName = value;
            }
        }

        public string SubSys
        {
            get
            {
                return subSys;
            }

            set
            {
                subSys = value;
            }
        }

        public string StatusID
        {
            get
            {
                return statusID;
            }

            set
            {
                statusID = value;
            }
        }

        public DateTime RsTime
        {
            get
            {
                return rsTime;
            }

            set
            {
                rsTime = value;
            }
        }

        public string ParamAddr
        {
            get
            {
                return paramAddr;
            }

            set
            {
                paramAddr = value;
            }
        }

        public string ParamValue
        {
            get
            {
                return paramValue;
            }

            set
            {
                paramValue = value;
            }
        }

        public DateTime CmdTime
        {
            get
            {
                return cmdTime;
            }

            set
            {
                cmdTime = value;
            }
        }

        public string Runned
        {
            get
            {
                return runned;
            }

            set
            {
                runned = value;
            }
        }

        public string StatusDescription
        {
            get
            {
                return statusDescription;
            }

            set
            {
                switch(value)
                {
                    case "停止":
                        {
                            this.Image = Respic.风扇正90;
                        }
                        break;
                    case "正转":
                        {
                            if (moveTimer.Enabled == false)
                            {
                                moveTimer.Start();
                            }
                            else
                            {
                                moveTimer.Stop();
                                moveTimer.Start();
                            }
                        }
                        break;
                    case "反转":
                        {
                            if (moveTimer.Enabled == false)
                            {
                                moveTimer.Start();
                            }
                            else
                            {
                                moveTimer.Stop();
                                moveTimer.Start();
                            }
                        }
                        break;
                    case "故障":
                        {
                            this.Image = Respic.风机故障;
                        }
                        break;
                    case "手动":
                        {
                            this.Image = Respic.风机故障;
                        }
                        break;
                }
                statusDescription = value;
            }
        }

        public string Zzid
        {
            get
            {
                return zzid;
            }

            set
            {
                zzid = value;
            }
        }

        public string Fzid
        {
            get
            {
                return fzid;
            }

            set
            {
                fzid = value;
            }
        }

        public string Tzid
        {
            get
            {
                return tzid;
            }

            set
            {
                tzid = value;
            }
        }

        public Fan()
        {
            InitializeComponent();
            this.StatusDescription = "停止";
            
            this.DeviceIdChanged += OnDeviceIdChanged;
            
        }

        private void OnDeviceIdChanged(object sender, EventArgs e)
        {
            string sqlSearchParamID = "select * from R_RunStatus where DeviceID='" + this.DeviceID + "'";
            bwSearchParamID.RunWorkerAsync(sqlSearchParamID);
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            fanName.Text = this.Name;

        }

        private void OpenTheFanForm()
        {
            if (StatusDescription != "故障" && StatusDescription != "手动" && Global.Role1 == "管理员")
            {
                FanForm fanForm = new FanForm(this);
                fanForm.StartPosition = FormStartPosition.CenterScreen;
                fanForm.ShowDialog();
            }
            if(Global.Role1 != "管理员")
            {
                MessageBox.Show("您不是管理员，无权限设置风机状态");
            }
            if(StatusDescription == "故障" || StatusDescription == "手动")
            {
                MessageBox.Show("当前风机状态不可控，请检查连接");
            }
        }

        private void Fan_DoubleClick(object sender, EventArgs e)
        {
            OpenTheFanForm();
        }

        private void formOpen_Click(object sender, EventArgs e)
        {
            OpenTheFanForm();
        }
        int flash = 0;
        private void moveTimer_Tick(object sender, EventArgs e)
        {
            switch(StatusDescription)
            {
                case "正转":
                    {
                        switch (flash)
                        {
                            case 0: this.Image = Respic.风扇正30; flash = 1; break;
                            case 1: this.Image = Respic.风扇正60; flash = 2; break;
                            case 2: this.Image = Respic.风扇正90; flash = 0; break;
                        }
                    }
                    break;
                case "反转":
                    {
                        switch (flash)
                        {
                            case 0: this.Image = Respic.风扇反60; flash = 1; break;
                            case 1: this.Image = Respic.风扇反30; flash = 2; break;
                            case 2: this.Image = Respic.风扇反90; flash = 0; break;
                        }
                    }
                    break;
                case "停止":
                    {
                        this.Image = Respic.风扇正90;
                        moveTimer.Stop();
                    }
                    break;
            }
        }

        private void bwSearchParamID_DoWork(object sender, DoWorkEventArgs e)
        {
            
            SqlDBHelper SDH = new SqlDBHelper();
            DataTable dtSearchParam = SDH.GetTable(e.Argument as string);
            e.Result = dtSearchParam;
        }

        private void bwSearchParamID_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            DataTable dtSearchParam = e.Result as DataTable;
            foreach(DataRow dr in dtSearchParam.Rows)
            {
                switch(dr["ParamStatus"].ToString())
                {
                    case "ZZ": Zzid = dr["ParamName"].ToString(); break;
                    case "FZ": Fzid = dr["ParamName"].ToString(); break;
                    case "TZ": Tzid = dr["ParamName"].ToString(); break;
                }
            }

        }
    }
}
